using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace Library.HttpTools
{
    public class HttpHelper
    {
        private const int BUFFER_SIZE = 1024;

        public static HttpWebResponse GetHttpResponse(string type, string url, string referrer, Dictionary<string, string> postVars, CookieCollection cookies, ICredentials credentials, int timeoutSecs)
        {
            String strContent = string.Empty;
            HttpWebRequest objRequest;

            foreach (string key in postVars.Keys)
            {
                if (strContent != string.Empty)
                {
                    strContent += "&";
                }
                strContent += HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(postVars[key]);
            }

            if (type.ToUpper() == "POST")
            {
                objRequest = (HttpWebRequest)WebRequest.Create(url);
                objRequest.AllowAutoRedirect = false;
                if (credentials != null) objRequest.Credentials = credentials;
                objRequest.Headers.Clear();
                objRequest.Method = type;
                objRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2";
                objRequest.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*" + "/" + "*;q=0.5";
                objRequest.Headers["Accept-Language"] = "en-us,en;q=0.5";
                objRequest.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
                objRequest.ContentType = "application/x-www-form-urlencoded";
                objRequest.KeepAlive = true;
                objRequest.Referer = referrer;

                objRequest.CookieContainer = new CookieContainer();
                if (cookies != null && cookies.Count > 0)
                {
                    objRequest.CookieContainer.Add(cookies);
                }

                objRequest.Timeout = timeoutSecs * 1000;
                objRequest.ContentLength = strContent.Length;
                objRequest.GetRequestStream().Write(System.Text.Encoding.ASCII.GetBytes(strContent), 0, strContent.Length); objRequest.GetRequestStream().Close();
            }
            else
            {
                objRequest = (HttpWebRequest)WebRequest.Create(url + ((strContent == string.Empty) ? "" : ("?" + strContent)));

                objRequest.AllowAutoRedirect = false;
                if (credentials != null) objRequest.Credentials = credentials;
                objRequest.Headers.Clear();
                objRequest.Method = type;
                objRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2";
                objRequest.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*" + "/" + "*;q=0.5";
                objRequest.Headers["Accept-Encoding"] = "gzip, deflate";
                objRequest.Headers["Accept-Language"] = "en-us,en;q=0.5";
                objRequest.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
                objRequest.Headers["Keep-Alive"] = "300";
                objRequest.ContentType = "application/x-www-form-urlencoded";
                objRequest.KeepAlive = true;
                objRequest.Referer = referrer;

                objRequest.CookieContainer = new CookieContainer();
                if (cookies != null && cookies.Count > 0)
                {
                    objRequest.CookieContainer.Add(cookies);
                }

                objRequest.Timeout = timeoutSecs * 1000;
            }

            HttpWebResponse response = objRequest.GetResponse() as HttpWebResponse;

            objRequest = null;

            return response;
        }

        public static int StreamToStream(Stream inputStream, Stream outputStream)
        {
            int length = 0;

            using (BinaryReader reader = new BinaryReader(inputStream))
            {
                BinaryWriter writer = new BinaryWriter(outputStream);

                byte[] buffer = new byte[BUFFER_SIZE];
                int bytesRead;

                do
                {
                    bytesRead = reader.Read(buffer, 0, BUFFER_SIZE);
                    length += bytesRead;

                    if (bytesRead == BUFFER_SIZE) writer.Write(buffer);
                    else if (bytesRead > 0) writer.Write(buffer, 0, bytesRead);
                    else break;

                } while (true);

                writer.Flush();
            }

            return length;
        }
    }
}
